/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.app.param.RechargePackageParam;
import com.yami.shop.bean.model.RechargePackage;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserLabel;
import com.yami.shop.bean.model.VipPackage;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.RechargePackageService;
import com.yami.shop.service.UserLabelService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import ma.glasnost.orika.MapperFacade;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2018/10/26.
 */
@RestController
@RequestMapping("/p/recharge")
@Api(tags = "充值接口")
public class RechargeController {

    @Autowired
    private UserService userService;
    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private RechargePackageService rechargePackageService;


    @GetMapping
    @ApiOperation(value = "获取充值套餐", notes = "获取充值套餐")
    public ResponseEntity<List<RechargePackage>> page() {
        List<RechargePackage> list = rechargePackageService.list();
        return ResponseEntity.ok(list);
    }


    @PostMapping
    @ApiOperation(value = "充值", notes = "充值 选择传套餐id或钻石数量")
    public ResponseEntity<Void> buy(@RequestBody @Param("vip套餐id") RechargePackageParam param) {
        String userId = SecurityUtils.getUser().getUserId();
        Integer packageId = param.getId();
        if (packageId != null) {
            RechargePackage p = rechargePackageService.getById(packageId);
            userService.addDiamond(userId, p.getDiamondCount());
        }else{
            Assert.notNull(param.getDiamondCount(),"充值钻石数量不能为空");
            if(param.getDiamondCount() == 0){
                throw new YamiShopBindException("充值钻石数量不能为0");
            }
            userService.addDiamond(userId, param.getDiamondCount());
        }

        return ResponseEntity.ok().build();
    }


}
